package cn.nawang.ebeim.ui.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

public class PcInfoUtil {
  /**
   * 
   * @author: huanghj
   * @Description: 获得内存空间大小
   * @param @return
   * @param @throws Exception
   * @return int
   * @date 2015年11月16日下午1:59:47
   *
   * @修改人
   * @修改日期
   */
  public static int memory() throws Exception {
    Sigar sigar = new Sigar();
    Mem mem = sigar.getMem();
    return (int) (mem.getTotal() / 1024l / 1024 / 1000);

  }

  public static String ethernet() throws Exception {
    Sigar sigar = new Sigar();
    String[] ifaces = sigar.getNetInterfaceList();
    InetAddress ia = null;
    try {
      ia = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    for (int i = 0; i < ifaces.length; i++) {
      NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
      if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())
          || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
          || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
        continue;
      }
      if (ia.getHostAddress().equals(cfg.getAddress())) {
        // System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址
        // System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址
        // System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址
        // System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码
        // System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息
        // System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//
        return cfg.getDescription();
      }
    }
    return null;
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获得处理器名称
   * @param @return
   * @param @throws Exception
   * @return String
   * @date 2015年11月16日下午1:57:46
   *
   * @修改人
   * @修改日期
   */
  public static String cpuProcess() throws Exception {
    Sigar sigar = new Sigar();
    CpuInfo infos[] = sigar.getCpuInfoList();
    String model = infos[0].getModel();
    return model;
  }

  // private void getHostName() throws Exception {
  // InetAddress s = InetAddress.getLocalHost();
  // System.out.println("计算机名"+s.getHostName());
  // }
  //
  public static String getSystemInfo() {
    Properties props = System.getProperties();
    System.out.println("操作系统的名称：" + props.getProperty("os.name"));
    return props.getProperty("os.name");
  }

  // 使用hyperic-sigar获取mac
  private static String getMac2() throws SigarException {
    Sigar sigar = new Sigar();
    String[] ifaces = sigar.getNetInterfaceList();
    for (String iface : ifaces) {
      NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(iface);
      if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())
          || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
          || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
        continue;
      }
      String mac = cfg.getHwaddr();
      return mac;
    }
    return null;

  }

  public static void main(String[] args) {
    try {
      
      System.out.println(memoryRate());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  public static String getCpuRate() {
    Sigar sigar = new Sigar();
    CpuPerc cpuList[] = null;
    try {
      cpuList = sigar.getCpuPercList();
    } catch (SigarException e) {
      e.printStackTrace();
    }
    CpuPerc cpu = cpuList[0];
    return CpuPerc.format(cpu.getUser());
  }
  
  
  public static float memoryRate() throws SigarException {
    Sigar sigar = new Sigar();
    Mem mem = sigar.getMem();
    float rate = (float) mem.getUsed() /  mem.getTotal()*100 ;
    return rate;
  }
  
}
